<?
include '../webinit.php';
include '../database/dbhelper.php';
$alert = "";
if (!empty($_POST["action"]))
    if ($_POST["action"] == 'add') {
        //entity
        $hoadon = new hoadon();
        $khachhang = new khachhang();
        $hoadonthanhtoan = new hoadonthanhtoan();
        //database
        $hoadondb = new hoadondb();
        $hoadonthanhtoandb = new hoadonthanhtoandb();
        //$khachhangdb = new khachhangdb();
        //get data
        $hoadon = $hoadondb->get_post_data();
        //$khachhang = $khachhangdb->get_post_data();
        $hoadonthanhtoan = $hoadonthanhtoandb->get_post_data();


        try {
            if ($hoadondb->insert($hoadon)) {
                
                $hoadonthanhtoan->hoadon = $hoadondb->get_last_insert();
                
                if ($hoadonthanhtoandb->insert($hoadonthanhtoan)) {
                    $alert = '<script type="text/javascript">  alert("Tạo hóa đơn thành công"); window.location = "' . $pageURL . '"; </script>';
                } else {
                    $alert = '<script type="text/javascript">  alert("Có lỗi xãy ra khi tạo hóa đơn!"); window.location = "' . $pageURL . '"; </script>';
                }
            } else {
                $alert = '<script type="text/javascript">  alert("Có lỗi xãy ra khi tạo hóa đơn!"); window.location = "' . $pageURL . '"; </script>';
            }
        } catch (Exception $e) {
            echo $e;
        }
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>

        <meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"/>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <link rel="stylesheet" href="../web/css/style.css"  type="text/css" />
        <link rel="stylesheet" href="../web/css/epoch_styles.css" type="text/css"/>
        <script type="text/javascript" src="../web/js/epoch_classes.js"></script>
        <script type="text/javascript">
            window.onload = function() {

                calendar1 = new Epoch('cal1','popup',document.getElementById('ngaythang'));

            };

            function hasWhiteSpace(s)
            {
                reWhiteSpace = new RegExp(/^\s+$/);
                if (reWhiteSpace.test(s)) {
                    //alert("Please Check Your Fields For Spaces");
                    return false;
                }
                return true;
            }

            function validateForm() {
                var msg = "Vui lòng kiểm tra các mục: \n-----------------------------\n";
                var check = true;
                if (!hasWhiteSpace(document.formHoaDon.ngaythang.value) || (document.formHoaDon.ngaythang.value == "")) {
                    msg  += "Ngày tháng\n";
                    check = false;
                    
                }
                if (!hasWhiteSpace(document.formHoaDon.tenkhachhang.value) || (document.formHoaDon.tenkhachhang.value == "")) {
                    msg  += "Tên khách hàng\n";
                    check = false;
                    
                }
                if (!hasWhiteSpace(document.formHoaDon.diachi.value) || (document.formHoaDon.diachi.value == "")) {
                    msg  += "Địa chỉ\n";
                    check = false;

                }
                if (!hasWhiteSpace(document.formHoaDon.sodienthoai.value) || (document.formHoaDon.sodienthoai.value == "")) {
                    msg  += "Số điện thoại\n";
                    check = false;

                }
                if (!hasWhiteSpace(document.formHoaDon.tenxe.value) || (document.formHoaDon.tenxe.value == "")) {
                    msg  += "Tên xe\n";
                    check = false;

                }
                if (!hasWhiteSpace(document.formHoaDon.sokhung.value) || (document.formHoaDon.sokhung.value == "")) {
                    msg  += "Số khung\n";
                    check = false;

                }
                if (!hasWhiteSpace(document.formHoaDon.somay.value) || (document.formHoaDon.somay.value == "")) {
                    msg  += "Số máy\n";
                    check = false;

                }
                if (!hasWhiteSpace(document.formHoaDon.gia.value) || (document.formHoaDon.gia.value == "") || (document.formHoaDon.gia.value <= 0)) {
                    msg  += "Giá xe\n";
                    check = false;

                }
                if (!hasWhiteSpace(document.formHoaDon.tonghoadonso.value) || (document.formHoaDon.tonghoadonso.value == "") || (document.formHoaDon.tonghoadonso.value <= 0)) {
                    msg  += "Tổng hóa đơn\n";
                    check = false;

                }
                //khach hang tra truoc
                if (!hasWhiteSpace(document.formHoaDon.sotien.value) || (document.formHoaDon.sotien.value == "")  || (document.formHoaDon.sotien.value <= 0)) {
                    msg  += "Trả trước\n";
                    check = false;

                }
                
                if (!check ) {
                    alert(msg);

                } else {
                    document.formHoaDon.gia.value = removeFormatCurrency(document.formHoaDon.gia.value);
                    document.formHoaDon.tonghoadonso.value = removeFormatCurrency(document.formHoaDon.gia.value);
                    //khach hang tra truoc
                    document.formHoaDon.sotien.value = removeFormatCurrency(document.formHoaDon.sotien.value);
                    
                }

                return check;
            }
        </script>
        <script type="text/javascript">
            <!-- Original:  Cyanide_7 (leo7278@hotmail.com) -->
            <!-- Web Site:  http://www7.ewebcity.com/cyanide7 -->

            <!-- This script and many more are available free online at -->
            <!-- The JavaScript Source!! http://javascript.internet.com -->

            <!-- Begin
            function formatCurrency(num) {
                num = num.toString().replace(/\$|\'/g,'');
                if(isNaN(num))
                    num = "0";
                sign = (num == (num = Math.abs(num)));
                num = Math.floor(num*100+0.50000000001);
                cents = num%100;
                num = Math.floor(num/100).toString();
                if(cents<10)
                    cents = "0" + cents;
                for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
                    num = num.substring(0,num.length-(4*i+3))+"'"+
                    num.substring(num.length-(4*i+3));
                return (((sign)?'':'-')  + num);
            }
            function removeFormatCurrency(num) {
                return num.toString().replace(/\$|\'/g,'');
            }
            //  End -->
        </script>

        <?= $alert ?>
        <title>Hoa don ban le </title>


    </head>
    <body>
        <div id="container">
            <? include '../web/header.php' ?>
            <div id="main">

                <h3>Tạo hóa đơn bán lẻ</h3>

                <form name="formHoaDon" action="" method="POST"  onsubmit="return validateForm();">
                    <input type="hidden" name="action" value="add"/>
                    <input type="hidden" name="isdeleted"  value="N"/>
                    <table class="fromTable">
                        <tr>
                            <td colspan="2" >
                                <hr/>
                            </td>
                        </tr>
                        <tr>
                            <td>Ngày tháng</td>
                            <td>
                                <input id="ngaythang" name="ngaythang" class="fromInputSize1" type="text" value=""/>
                                <input type="button" onclick="calendar1.toggle();" value="Chọn"/>
                            </td>
                        </tr>
                        <tr style="display: none">
                            <td>LoaiHoaDon</td>
                            <td><input name="loaihoadon" type="text" value="Xuat"/></td>
                        </tr>
                        <tr style="display: none">
                            <td>KhachHangID</td>
                            <td><input name="khachhangid" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td colspan="2" >
                                <hr/>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" >
                                <h4> Thông tin khách hàng</h4>
                            </td>
                        </tr>

                        <tr>
                            <td>Tên khách hàng</td>
                            <td><input name="tenkhachhang" class="fromInputSize2" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td>Địa chỉ</td>
                            <td>
                                <input name="diachi" class="fromInputSize4" type="text" value=""/>
                            </td>
                        </tr>
                        <tr>
                            <td>Số điện thoại</td>
                            <td><input name="sodienthoai" class="fromInputSize2" type="text" value=""/></td>
                        </tr>
                        <tr style="display: none">
                            <td>SoTaiKhoan</td>
                            <td><input name="sotaikhoan" class="fromInputSize2" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td colspan="2" >
                                <hr/>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <h4>Thông tin hóa đơn</h4>
                            </td>
                        </tr>
                        <tr>
                            <td>Tên xe</td>
                            <td><input name="tenxe" class="fromInputSize2" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td>Số khung</td>
                            <td><input name="sokhung" class="fromInputSize1" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td>Số máy</td>
                            <td><input name="somay"  class="fromInputSize1" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td>Giá</td>
                            <td><input name="gia"  class="fromInputMoney" type="text" value="0" onkeyup="this.value = formatCurrency(this.value)" /></td>
                        </tr>
                        <tr style="display: none">
                            <td>Số đăng kiểm</td>
                            <td><input name="sodangkiem" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td colspan="2" >
                                <hr/>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <h4>Thanh toán</h4>
                            </td>
                        </tr>
                        <tr>
                            <td>Hình thức thanh toán</td>
                            <td>
                                <input name="phuongthucthanhtoan"  type="radio" value="TraDu" checked="true"/> Trả đủ &nbsp;&nbsp;
                                <input name="phuongthucthanhtoan"  type="radio" value="TraGop"/>  Trả góp &nbsp;&nbsp;
                                <input name="phuongthucthanhtoan"  type="radio" value="No"/>  Nợ ngắn hạn
                            </td>
                        </tr>
                        <tr>
                            <td>Tổng hóa đơn</td>
                            <td>
                                <input name="tonghoadonso" class="fromInputMoney" type="text" value="0"
                                       onkeyup="this.value = formatCurrency(this.value)"    />
                            </td>
                        </tr>
                        <tr>
                            <td>Trả trước</td>
                            <td>
                                <!--table hoa don thanh toan => sotien -->
                                <input name="sotien" class="fromInputMoney" type="text" value="0" onkeyup="this.value = formatCurrency(this.value)" />
                            </td>
                        </tr>
                        <tr>
                            <td>Còn lại</td>
                            <td>
                                <input name="conlai" class="fromInputMoney" type="text" value="0"  onkeyup="this.value = formatCurrency(this.value)" />
                                <input name="tinhtoan" type="button" value="Tính"  onclick=" 
                                    document.formHoaDon.conlai.value =  formatCurrency(removeFormatCurrency(document.formHoaDon.tonghoadonso.value) - removeFormatCurrency(document.formHoaDon.sotien.value));"/>

                            </td>
                        </tr>
                        <tr style="display: none">
                            <td>TongHoaDonChu</td>
                            <td><input name="tonghoadonchu" type="text" value=""/></td>
                        </tr>
                        <tr style="display: none">
                            <td>ThueGTGT</td>
                            <td><input name="thuegtgt" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td colspan="2" >
                                <hr/>
                            </td>
                        </tr>
                        <tr>
                            <td>Thời hạn (Tháng)  </td>
                            <td><input name="thoihan" class="fromInputSize1" type="text" value=""/> </td>
                        </tr>
                        <tr>
                            <td>Lãi suất (%)</td>
                            <td><input name="laisuat" class="fromInputSize1" type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td>Tiền định kỳ</td>
                            <td><input name="tiendinhky" class="fromInputSize1"  type="text" value=""/></td>
                        </tr>
                        <tr>
                            <td valign="top">Ghi chú</td>
                            <td>
                                <textarea cols="" rows=""  name="ghichu" class="fromInputSizeBig" >
                                </textarea>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" >
                                <hr/>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <input type="submit" class="fromButton" value="Lưu lại"/>
                                <input type="button" class="fromButton" value="Bản in xem thử"/>
                                <input type="button" class="fromButton" value="In ra"/>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <? include '../web/footer.php' ?>
        </div>
    </body>
</html>
